package com.swpu.seckill.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;

import java.io.UnsupportedEncodingException;


/**
 * The type Md 5 util.
 */
public class MD5Util {

    /**
     * Md 5 string.
     *
     * @param str the str
     * @return the string
     */
    public static String md5(String str){
        return DigestUtils.md5Hex(str);
    }

    private static final String SALT = "1a2b3c4d";


    /**
     * 第一次加密
     *
     * @param inputPass
     * @return java.lang.String
     * @author Oxygen
     * @operation add
     **/
    public static String inputPassToFromPass(String inputPass) {
        String str = inputPass + SALT;
        return md5(str);
    }

    /**
     * 第二次加密
     * @author Oxygen
     * @operation add
     * @param formPass
     * @param salt
     * @return java.lang.String
     **/
    public static String formPassToDBPass(String formPass, String salt) {
        String str = formPass + salt;
        return md5(str);
    }

    /**
     * 真正使用的加密方法
     * @author Oxygen
     * @param inputPass
     * @param salt
     * @return java.lang.String
     */
    public static String inputPassToDBPass(String inputPass, String salt) {
        String fromPass = inputPassToFromPass(inputPass);
        String dbPass = formPassToDBPass(fromPass, salt);
        return dbPass;
    }

    @Test
    public void test() {
        String first = inputPassToFromPass("754709489");
        System.out.println(first);
        System.out.println(formPassToDBPass(first, SALT));
        System.out.println(inputPassToDBPass("754709489", SALT));
    }

}
